package org.grow.present.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.grow.present.domain.ComDetails;
import org.grow.present.domain.Company;
import org.grow.present.interceptor.AuthInterceptor;
import org.grow.present.service.ComDetailsService;
import org.grow.present.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/comDetails")
public class ComDetailsController {
    @Autowired
    private ComDetailsService comDetailsService;
    @Autowired
    private CompanyService companyService;

    @PostMapping("/saveCurrentComDetails")
    public ComDetails saveCurrentComDetails(@RequestBody ComDetails comDetails){
        Integer uid = AuthInterceptor.getUID();
        QueryWrapper<ComDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("co_id",uid);
        ComDetails db = comDetailsService.getOne(queryWrapper);
        if (db == null) {
            comDetails.setCId(0);
            comDetails.setCoId(uid);
            comDetailsService.save(comDetails);
        } else {
            comDetails.setCId(db.getCId());
            comDetails.setCoId(db.getCoId());
            comDetailsService.updateById(comDetails);
        }
        ComDetails one = comDetailsService.getOne(queryWrapper);

//        终结 认证流程 更改company表中status字段 3 =》4
        Company currentCompany = companyService.getById(uid);
//        可以限制只有未实名状态下才可以进行
//        if(currentCompany.getComStatus()!='3'){
//            throw new Exception("xxx")
//        }
        currentCompany.setComStatus("4");
        companyService.updateById(currentCompany);


        return  one;
    }
}
